<template>
  <el-row>
    <el-col :span="24">
      <el-card class="search-form">
        <el-form ref="searchForm" class="el-form-col el-form-s" :model="search">
          <el-row :gutter="15">
            <el-col :lg="4" :md="6">
              <el-form-item prop="loginName">
                <el-input v-model="search.loginName" placeholder="登录账号" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="name">
                <el-input v-model="search.name" placeholder="姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item prop="position">
                <el-input v-model="search.position" placeholder="岗位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit" @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-common-user/childUser/findChildUserList"
        :initParams="{
          projectType: projectType
        }"
        :columns="columns">
        <template slot="buttons">
          <el-button v-if="credentialDetail.isCredential" type="primary" @click="add">新增</el-button>
        </template>
        <template slot="column">
          <el-table-column fixed="right" width="100px" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="stopAndStart(scope.row, 'start')"
                v-if="scope.row.state === 0"
              >
                启用
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="stopAndStart(scope.row, 'stop')"
                v-if="scope.row.state === 1"
              >
                停用
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="show(scope.row)"
              >
                详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import {copy, confirm} from '@/utils'
import {unixTimestampFilter} from '@/filters'
import {getUserInfo} from '@/utils/auth'
export default {
  name: 'fhbAccountIndex',
  props: {
    projectType: {
      type: String,
      default: process.env.projectType
    }
  },
  data() {
    let columns = [
      {
        label: '登录账号',
        data: 'loginName'
      },
      {
        label: '姓名',
        data: 'name'
      },
      {
        label: '岗位',
        data: 'position'
      },
      {
        label: '创建人',
        data: 'foundUserName'
      },
      {
        label: '创建时间',
        data: 'foundtime',
        width: '134px',
        render(data, full) {
          return unixTimestampFilter(data)
        }
      },
      {
        label: '状态',
        data: 'state',
        render(data, full) {
          switch (data) {
            case 1: return '启用'
            case 0: return '停用'
            case '1': return '启用'
            case '0': return '停用'
            default: return '未知'
          }
        }
      }
    ]
    return {
      routeName: this.$route.name,
      positionArr: [],
      companyArr: [],
      roleArr: [],
      isOpen: false,
      visible: false,
      searchLoading: false,
      search: {
        loginName: '',
        name: '',
        position: ''
      },
      columns,
      tabs: [],
      credentialDetail: {}
    }
  },
  created() {
    this.isPassCompanyAuthentication()
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    isPassCompanyAuthentication() {
      this.$ajax.get('/ms-fahuobao-user/OutGoodsUser/is-mentCredential', {userId: getUserInfo('id')}).then(response => {
        if (response.success) {
          this.credentialDetail = response.data || {}
        }
      })
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    show(data) {
      this.$router.push({path: '/ss-fhb/account/show/' + data.id})
    },
    add() {
      this.$router.push({path: '/ss-fhb/account/create'})
    },
    stopAndStart(data, operation) {
      let title = ''
      let state = ''
      let msg = ''
      if (operation === 'stop') {
        title = '停用'
        state = '0'
        msg = '<p>确认要停用该账号吗？</p><p>停用后账号将不能再登录系统</p>'
      } else {
        title = '启用'
        state = '1'
        msg = '<p>确定要启用该账号吗？</p><p>启用后账号对应的权限将开放使用。</p>'
      }
      confirm(msg, title, {
        confirmButtonLoading: true,
        type: 'warning',
        dangerouslyUseHTMLString: true,
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          return this.$ajax.post('/ms-common-user/user/start-up-stop-user', {
            userId: data.id,
            state: state
          }).then(response => {
            if (response.success) {
              close().then(_ => {
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
                this.refresh()
              })
            } else {
              confirmButtonLoadingClose()
            }
          }).finally(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    }
  }
}
</script>
